'use strict';
module.exports = {
	/**
	 * 批量发送订阅消息
	 */
	main: async (event) => {
		const db = uniCloud.database();
		var nowtime = new Date().getTime()
		let { data = {}, userInfo, util, filterResponse, originalParam } = event;
		let { customUtil, uniID, config, pubFun, vk, _ } = util;
		let { uid, appid, msgs, things } = data;
		let res = {
			code: 0,
			msg: ""
		};

		//通过appid查询对应的appsecret
		const app = await db.collection('a_appid').where({ appid: appid }).get()
		let appsecret = app.data[0].appsecret

		//获取对应的模板ID
		let jjgx_Template = app.data[0].jjgx
		let xspsx_Template = app.data[0].xspsx
		let flhd_Template = app.data[0].flhd

		//====token获取====
		let access_token = await vk.openapi.weixin.auth.getAccessToken({
			appid: appid,
			appsecret: appsecret
		});
		console.log("access_token=", access_token);
		//====获取token结束====

		// 业务逻辑开始-----------------------------------------------------------
		// console.log("things=", things);
		let batchRunRes = await vk.pubfn.batchRun({
			// 主执行函数
			main: async (item, index) => {
				await vk.pubfn.sleep((Math.floor(Math.random() * (3 - 0)) + 0) * 100);

				//====判断消息类型，执行不同函数====
				if (item.msgType == "jjgx") {
					jjgx(access_token, item.openid, jjgx_Template)
				}
				if (item.msgType == "flhd") {
					flhd(access_token, item.openid, flhd_Template)
				}
				if (item.msgType == "xspsx") {
					xspsx(access_token, item.openid, xspsx_Template)
				}
				//====判断消息类型结束====

				//====更改状态为已发送====
				const updtate = await db.collection('a_dingyue').doc(item._id).update({
					is_send: true
				})
				//====更改状态结束====

				return {
					code: 0,
					index
				}
			},
			// 最大并发量，如果设置为1，则会按顺序执行
			concurrency: 100,
			// 数据源，这些数据会依次跑一遍main函数
			data: msgs
		});


		// 业务逻辑结束-----------------------------------------------------------
		return res;

		async function xspsx(access_token, openid, xspsx_Template) {
			// 通知
			const pushmsg = {
				"touser": openid,
				"miniprogram_state": "formal",
				"page": things.path || "/pages/index/index",
				"template_id": xspsx_Template,
				"data": {
					"thing1": {
						"value": things.thing1
						// "value": "我是名称2"
					},
					"phrase2": {
						"value": things.phrase2
						// "value": "剧集类型"
					},
					"thing3": {
						"value": things.thing3
						// "value": "剧集简介"
					}
				}
			}
			tosend(pushmsg, access_token)
		}

		async function jjgx(access_token, openid, jjgx_Template) {
			const pushmsg = {
				"touser": openid,
				"miniprogram_state": "formal",
				"page": things.path || "/pages/index/index",
				"template_id": jjgx_Template,
				"data": {
					"thing1": {
						// "value": "西游记"
						"value": things.thing1
					},
					"thing2": {
						"value": things.thing2
						// "value": "更新22集"
					},
					"time3": {
						"value": things.time3
						// "value": "2020年05月12日 12:00"
					},
					"thing4": {
						"value": things.thing4
						// "value": "剧集更新"
					},
				}
			}
			tosend(pushmsg, access_token)
		}

		async function flhd(access_token, openid, flhd_Template) {
			const pushmsg = {
				"touser": openid,
				"miniprogram_state": "formal",
				"page": things.path || "/pages/index/index",
				"template_id": flhd_Template,
				"data": {
					"thing5": {
						"value": things.thing5
						// "value": "我是名称2"
					},
					"thing4": {
						"value": things.thing4
						// "value": "我是介绍1"
					},
					"time3": {
						"value": things.time3
						// "value": "2022年6月1日"
					},
					"thing2": {
						"value": things.thing2
						// "value": "温馨提示哦"
					},
				}
			}
			tosend(pushmsg, access_token)
		}

		async function tosend(pushmsg, access_token) {
			console.log("thing169=", things);
			let sendurl = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" +
				access_token;
			const send = await uniCloud.httpclient.request(sendurl, {
				method: 'POST',
				data: JSON.stringify(pushmsg),
				contentType: 'json', // 指定以application/json发送data内的数据
				dataType: 'json' // 指定返回值为json格式，自动进行parse
			})
			console.log("send=", send);
		}
	}
}
